using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;

//Generated on 24/06/2007 16:38:57 by Rupert

namespace SubSonicMembershipProvider.DataObjects{
    /// <summary>
    /// Strongly-typed collection for the UserRole class.
    /// </summary>
    [Serializable]
    public partial class UserRoleCollection : ActiveList<UserRole, UserRoleCollection> 
    {       
        public UserRoleCollection() {}

    }

    /// <summary>
    /// This is an ActiveRecord class which wraps the UserRole table.
    /// </summary>
    [Serializable]
    public partial class UserRole : ActiveRecord<UserRole>
    {
    
	    #region Default Settings
	    protected static void SetSQLProps() 
	    {
		    GetTableSchema();
	    }

	    #endregion
        #region Schema Accessor
	    public static TableSchema.Table Schema
        {
            get
            {
                if (BaseSchema == null)
                {
                    SetSQLProps();
                }

                return BaseSchema;
            }

        }

    	
        private static void GetTableSchema() 
	    {
            if(!IsSchemaInitialized)
            {
                //Schema declaration
				TableSchema.Table schema = new TableSchema.Table("UserRole", TableType.Table, DataService.GetInstance("MembershipProvider"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = @"dbo";
                //columns
                
                TableSchema.TableColumn colvarUserRoleId = new TableSchema.TableColumn(schema);
                colvarUserRoleId.ColumnName = "UserRoleId";
                colvarUserRoleId.DataType = DbType.Int32;
                colvarUserRoleId.MaxLength = 0;
                colvarUserRoleId.AutoIncrement = true;
                colvarUserRoleId.IsNullable = false;
                colvarUserRoleId.IsPrimaryKey = false;
                colvarUserRoleId.IsForeignKey = false;
                colvarUserRoleId.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarUserRoleId);
                
                TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
                colvarUserId.ColumnName = "UserId";
                colvarUserId.DataType = DbType.Int32;
                colvarUserId.MaxLength = 0;
                colvarUserId.AutoIncrement = false;
                colvarUserId.IsNullable = false;
                colvarUserId.IsPrimaryKey = true;
                colvarUserId.IsForeignKey = true;
                colvarUserId.IsReadOnly = false;
                
                
				colvarUserId.ForeignKeyTableName = "UserMembership";
                
                schema.Columns.Add(colvarUserId);
                
                TableSchema.TableColumn colvarRoleId = new TableSchema.TableColumn(schema);
                colvarRoleId.ColumnName = "RoleId";
                colvarRoleId.DataType = DbType.Int32;
                colvarRoleId.MaxLength = 0;
                colvarRoleId.AutoIncrement = false;
                colvarRoleId.IsNullable = false;
                colvarRoleId.IsPrimaryKey = true;
                colvarRoleId.IsForeignKey = true;
                colvarRoleId.IsReadOnly = false;
                
                
				colvarRoleId.ForeignKeyTableName = "Role";
                
                schema.Columns.Add(colvarRoleId);
                
                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["MembershipProvider"].AddSchema("UserRole",schema);
            }

        }

        #endregion
        
        #region Query Accessor
	    public static Query CreateQuery()
	    {
		    return new Query(Schema);
	    }

	    #endregion
	    
	    #region .ctors
	    public UserRole()
	    {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        
        public UserRole(bool useDatabaseDefaults)
	    {
		    SetSQLProps();
		    if(useDatabaseDefaults)
		    {
				ForceDefaults();
			}

			MarkNew();
	    }

	    public UserRole(object keyID)
	    {
		    SetSQLProps();
            SetDefaults();
		    LoadByKey(keyID);
	    }

    	 
	    public UserRole(string columnName, object columnValue)
        {
            SetSQLProps();
            SetDefaults();
            LoadByParam(columnName,columnValue);
        }

        
	    #endregion
	    
	    #region Props
	    
          
        [XmlAttribute("UserRoleId")]
        public int UserRoleId 
	    {
		    get { return GetColumnValue<int>("UserRoleId"); }

            set 
		    {
			    SetColumnValue("UserRoleId", value);
            }

        }

	      
        [XmlAttribute("UserId")]
        public int UserId 
	    {
		    get { return GetColumnValue<int>("UserId"); }

            set 
		    {
			    SetColumnValue("UserId", value);
            }

        }

	      
        [XmlAttribute("RoleId")]
        public int RoleId 
	    {
		    get { return GetColumnValue<int>("RoleId"); }

            set 
		    {
			    SetColumnValue("RoleId", value);
            }

        }

	    
	    #endregion
	    
	    
	 	
			
	    
	    
	    
	    #region ForeignKey Properties
	    
        /// <summary>
        /// Returns a UserMembership ActiveRecord object related to this UserRole
        /// 
        /// </summary>
	    public SubSonicMembershipProvider.DataObjects.UserMembership UserMembership
        {
	        get { return SubSonicMembershipProvider.DataObjects.UserMembership.FetchByID(this.UserId); }

	        set
	        {
	        	SetColumnValue("UserId", value.UserId);
		        //SetColumnValue("UserId", value.UserId);
	        }

        }

	    
	    
        /// <summary>
        /// Returns a Role ActiveRecord object related to this UserRole
        /// 
        /// </summary>
	    public SubSonicMembershipProvider.DataObjects.Role Role
        {
	        get { return SubSonicMembershipProvider.DataObjects.Role.FetchByID(this.RoleId); }

	        set
	        {
	        	SetColumnValue("RoleId", value.RoleId);
		        //SetColumnValue("RoleId", value.RoleId);
	        }

        }

	    
	    
	    #endregion
	    
	    
	    
	    //no ManyToMany tables defined (0)
	    
	    #region ObjectDataSource support
    	
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Insert(int varUserId,int varRoleId)
	    {
		    UserRole item = new UserRole();
		    
            item.UserId = varUserId;
            
            item.RoleId = varRoleId;
            
	    
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Update(int varUserRoleId,int varUserId,int varRoleId)
	    {
		    UserRole item = new UserRole();
		    
                item.UserRoleId = varUserRoleId;
				
                item.UserId = varUserId;
				
                item.RoleId = varRoleId;
				
		    item.IsNew = false;
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

	    #endregion
	    #region Columns Struct
	    public struct Columns
	    {
		    
		    
            public static string UserRoleId = @"UserRoleId";
            
            public static string UserId = @"UserId";
            
            public static string RoleId = @"RoleId";
            
	    }

	    #endregion
    }

}

